package com.linkage.educloud.ah.view.activity.topic;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.adapter.topic.TopicMineAdapter;
import com.linkage.educloud.ah.bean.TopicChildBean;
import com.linkage.educloud.ah.mvp.contract.topic.ITopicMineContract;
import com.linkage.educloud.ah.mvp.presenter.topic.TopicMinePresenter;
import com.linkage.educloud.ah.view.BasicTitleActivity;
import com.zzhou.common.library.util.ToastShow;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Mr'Tang on 2018/12/7.
 */

public class TopicMineActivity extends BasicTitleActivity implements View.OnClickListener, ITopicMineContract.IView {

    @BindView(R.id.ac_topic_mine_release)
    TextView ac_topic_mine_release;
    @BindView(R.id.ac_topic_mine_reply)
    TextView ac_topic_mine_reply;
    @BindView(R.id.common_recylerview)
    XRecyclerView common_recylerview;

    int arg = 1;//1--目前在我发布的  2--我回复的
    int page = 1;
    TopicMineAdapter adapter;
    List<TopicChildBean> list = new ArrayList<>();
    TopicMinePresenter iPresenter;

    public static void launch(Context context) {
        Intent intent = new Intent(context, TopicMineActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_topic_mine;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("我的帖子");
        ac_topic_mine_release.setOnClickListener(this);
        ac_topic_mine_reply.setOnClickListener(this);
        iPresenter = new TopicMinePresenter(this, this);
        initRecylerview();

        iPresenter.getMyCards(page);
    }

    private void initRecylerview() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        common_recylerview.setLayoutManager(layoutManager);
        common_recylerview.setItemAnimator(new DefaultItemAnimator());
        common_recylerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        common_recylerview.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        common_recylerview.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                if (arg == 1) {
                    iPresenter.getMyCards(page);
                } else {
                    iPresenter.getMyReplyCards(page);
                }
            }

            @Override
            public void onLoadMore() {
                page++;
                if (arg == 1) {
                    iPresenter.getMyCards(page);
                } else {
                    iPresenter.getMyReplyCards(page);
                }
            }
        });

        adapter = new TopicMineAdapter(this, list, arg);
        common_recylerview.setAdapter(adapter);
    }

    @Override
    public void initRecylerviewDate(List<TopicChildBean> mlist) {
        if (mlist.size() == 0) {
            ToastShow.show(this, "暂未查询到您的帖子!");
        }
        if (mlist.size() < 10) {
            common_recylerview.setLoadingMoreEnabled(false);
        } else {
            common_recylerview.setLoadingMoreEnabled(true);
        }
        adapter.addAll(mlist, page != 1);
    }

    public void stopRefresh() {
        common_recylerview.loadMoreComplete();
        common_recylerview.refreshComplete();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ac_topic_mine_release:
                if (arg == 1) {
                    return;
                }
                arg = 1;
                page = 1;
                ac_topic_mine_release.setTextColor(getResources().getColor(R.color.topic_bg));
                ac_topic_mine_reply.setTextColor(getResources().getColor(R.color.btn_gray_normal));
                iPresenter.getMyCards(page);
                break;
            case R.id.ac_topic_mine_reply:
                if (arg == 2) {
                    return;
                }
                arg = 2;
                page = 1;
                ac_topic_mine_release.setTextColor(getResources().getColor(R.color.btn_gray_normal));
                ac_topic_mine_reply.setTextColor(getResources().getColor(R.color.topic_bg));
                iPresenter.getMyReplyCards(page);
                break;
        }
    }
}
